<%
var title=user.realName+"人员信息维护";
layout("/layouts/blank.html",{title:title}){
%>
<style type="text/css">
    .required {
        color: red;
    }
</style>
<section class="content" id="container" v-cloak>
    <div class="col-sm-2" style="min-width: 150px;">
        <div class="ztree" id="dbTree"></div>
    </div>
    <div class="col-sm-10">
        <div id="listTable" lay-filter='listTable'></div>
    </div>
</section>
<script type="text/html" id="AttachTypeTpl">
    <div style="text-align: center">
        {{#
        var data=d[d.LAY_FIELD]!=null?d[d.LAY_FIELD]:0;
        var num=data!=0?(data.split(",").length):0;
        }}
        {{#if(num>0){ }}
        <div class="layui-btn layui-btn-xs layui-btn-normal" lay-event="showAttach" data-field="{{d.LAY_FIELD}}">查看附件({{num}})</div>
        {{# }else{ }}
        无附件
        {{# } }}
    </div>
</script>
<script type="text/html" id="AttachTypeImgTpl">
    <div style="text-align: center">
        {{#
        var data=d[d.LAY_FIELD]!=null?d[d.LAY_FIELD]:0;
        var num=data!=0?(data.split(",").length):0;
        }}
        {{#if(num>0){ }}
        <div class="layui-btn layui-btn-xs layui-btn-normal" lay-event="showAttach" data-field="{{d.LAY_FIELD}}">查看图片({{num}})</div>
        {{# }else{ }}
        无图片
        {{# } }}
    </div>
</script>
<script type="text/javascript">
    var listTable, table, dbTree;
    var win = window.parent.parent || window.parent || window;
    var vm = new Vue({
        el: '#container',
        data: {
            fromData: {},
            selectNode: undefined,
            fromDataSubmit: false,

        },
        methods: {
            handleDel: function () {
                var data = dbTree.getSelected();
                var listData = this.getCheckData();
                if (listData.length > 0) {
                    var arr = new Array();
                    for (var i in listData) {
                        arr.push(listData[i].id);
                    }
                    core.confirm("确定删除选中的记录吗？一旦删除将无法恢复！", function () {
                        var json = core.postJSON("/sysDynamicFrom/delToReview", {
                            tableId: data.tableId,
                            sourceIds: arr.join(",")
                        });
                        win.core.msg(json);
                        if (json.ok) {
                            listTable.reload();
                        }
                    });
                } else {
                    core.error("请先选择一条记录！");
                }
            },
            handleEdit: function () {
                var data = dbTree.getSelected();
                var listData = this.getCheckData();
                if (listData.length == 1) {
                    win.core.openUrlOk("/sysDynamicFrom/editToReview?tableId=" + data.tableId + "&userid=${user.id}&sourceId=" + listData[0].id, "编辑记录", '80%', '80%', function (body, index) {
                        var iframeVm = win.window.frames['layui-layer-iframe' + index].window.vm;
                        var json = iframeVm.handleSave();
                        if (json.ok) {
                            win.layer.close(index);
                            win.core.msg(json);
                            listTable.reload();
                        } else {
                            win.core.msg(json);
                        }
                    });
                } else if (listData.length == 0) {
                    core.error("请选择一条记录！");
                } else if (listData.length > 1) {
                    core.error("请勿选择多条记录！");
                }
            },
            handleSave: function () {
                core.msg("开发中...")
            },
            handleAdd: function () {
                var data = dbTree.getSelected();
                var json = core.postJSON("/sysDynamicFrom/canAdd", {
                    tableId: data.tableId,
                    userid: "${user.id!}"
                });
                if (json.data) {
                    win.core.openUrlOk("/sysDynamicFrom/editToReview?tableId=" + data.tableId + "&userid=${user.id}", "新增记录", '80%', '80%', function (body, index) {
                        var iframeVm = win.window.frames['layui-layer-iframe' + index].window.vm;
                        var json = iframeVm.handleSave();
                        if (json.ok) {
                            win.layer.close(index);
                            win.core.msg(json);
                            listTable.reload();
                        } else {
                            win.core.msg(json);
                        }
                    });
                } else {
                    core.error(json.msg);
                }
            },
            getCheckData: function () {
                return table.checkStatus('listTable').data;
            }
        },
        created: function () {

        },
        updated: function () {
        },
        mounted: function () {
            dbTree = new showZtree({
                el: "#dbTree",
                url: "/sysDataTable/authTable",
                data: {id: "id", name: "text"},
                onSelect: function (node) {
                    if (node.type == 0) {
                        var json = core.postJSON("/sysUserMaintain/getCols", {tableid: node.id});
                        listTable.reload({
                            cols: [json.data],
                            method: 'post',
                            url: base + '/sysUserMaintain/queryUserDatalistPage',
                            where: {tableid: node.id, userid: '${user.id}'},
                            done:function () {
                                $(".toptoolbar").removeClass("layui-btn-disabled");
                            }
                        });
                        $(".toptoolbar").removeClass("layui-btn-disabled");
                    } else {
                        $(".toptoolbar").addClass("layui-btn-disabled");
                        return false;
                    }
                }
            });
            layui.use(['table'], function () {
                table = layui.table;
                listTable = table.render({
                    id: 'listTable',
                    elem: '#listTable',
                    page: true,
                    method: 'post',
                    height: 'full-80',
                    limit: 50,
                    even: true,
                    size: 'sm',
                    toolbar: '#topToolbar',
                    defaultToolbar: ['filter'],
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize',
                    },
                    cols: [[
                        {title: '查询结果', type: 'numbers', width: 150},
                    ]],
                });
                table.on("tool(listTable)", function (obj) {
                    var data = obj.data, layEvent = obj.event;
                    if (layEvent == "showAttach") {
                        var dataField = $(obj.tr).find("div[lay-event=showAttach]").attr("data-field");
                        core.showAttachList(data[dataField]);
                    }
                });
                table.on("toolbar(listTable)", function (obj) {
                    var layEvent = obj.event;
                    if (!$(this).hasClass("layui-btn-disabled")) {
                        switch (layEvent) {
                            case "handleAdd":
                                vm.handleAdd();
                                break;
                            case "handleEdit":
                                vm.handleEdit();
                                break;
                            case "handleDel":
                                vm.handleDel();
                                break;
                        }
                    }
                });

            });
        }
    });
</script>
<script type="text/html" id="topToolbar">
    <div class="btn toptoolbar layui-btn-disabled layui-btn-primary layui-btn-sm" lay-event="handleAdd">新增</div>
    <div class="btn toptoolbar layui-btn-disabled layui-btn-primary layui-btn-sm" lay-event="handleEdit">编辑</div>
    <div class="btn toptoolbar layui-btn-disabled layui-btn-primary layui-btn-sm" lay-event="handleDel">删除</div>
</script>
<%}%>